package cn.wolfcode.luowowo.mgrsite.web.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatisVOService travelStatisVOService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("-------------redis数据预热开始-------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail strategyDetail : list) {
            //还需要判断当前key是否已经在redis缓存中，防止被数据覆盖
            strategyStatisVOService.setStrategyStatisVO(strategyDetail);
        }
        //Travel的vo数据
        List<Travel> travelList = travelService.list();
        for (Travel travel : travelList) {
            travelStatisVOService.setTravelStatisVo(travel);
        }
        System.out.println("-------------redis数据预热结束-------------------");

        System.out.println("--------------zset数据预热开始-------------------");
        for (StrategyDetail detail : list) {
            String voKey = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            if(strategyStatisVOService.isExistAttrZset(voKey,zsetKey)){
                continue;
            }
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVOService.addHotCommendScore(detail.getId(),score);
        }
        for (StrategyDetail detail : list) {
            String voKey = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = null;
            if(detail.isIsabroad()){
                zsetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else{
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }
            if(strategyStatisVOService.isExistAttrZset(voKey,zsetKey)){
                continue;
            }
            int score = detail.getFavornum() + detail.getThumbsupnum();
            strategyStatisVOService.addAbroadAndChinaScore(detail.getId(),score);
        }
        System.out.println("--------------zset数据预热结束-------------------");
    }
}
